package com.latam.arq.example.web.managedbeans;


 

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.latam.arq.commons.exceptions.LATAMException;
import com.latam.arq.commons.web.utils.MessageUtil;
import com.latam.arq.example.crud.ejb.services.PamCRUDService;
import com.latam.arq.example.domain.PamBean;

 
@ManagedBean(name = "dfView")
@ViewScoped
public class DFView {
	
	private static final Logger logger = LoggerFactory.getLogger(DFView.class);
         
	@EJB
	private PamCRUDService pamEJBServices;
	
	 private PamBean cars ;
	 
	 
	 @PostConstruct
	  public void init() {
		 			this.cars = new PamBean();
		
	 }
	
    public PamBean getCars() {
		return cars;
	}

	public void setCars(PamBean cars) {
		this.cars = cars;
	}

	public void chooseCar() {
        RequestContext.getCurrentInstance().openDialog("selectorMedicoForm");
        logger.debug("open dialog");
        logger.info("open dialog");

    }
	

    public void onCarChosen(SelectEvent event) {
    	
    	logger.debug("onCarChosen");
    	
    	 this.cars  = (PamBean) event.getObject();
//        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Car Selected", "Id:" + car.getId());
//         
//        FacesContext.getCurrentInstance().addMessage(null, message);
         
 
        
    }
    
	/**
	 * (FacesEvent)
	 * @param e
	 */
	public void save(ActionEvent evt){
		try{
			pamEJBServices.save(cars);
			MessageUtil.addInfoMessage("exampleBeanCRUD.action.save",null);
		}catch(final LATAMException e){
			if (logger.isErrorEnabled()){
				logger.error(e.getMessage());
			}
			MessageUtil.addErrorMessage(e);
		}
		init();
	}
	


}